//
//  PayView.m
//  HAHAWENWAN
//
//  Created by 李小龙 on 2017/9/27.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "PayView.h"
@interface PayView ()<UITextFieldDelegate>

@end

@implementation PayView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self createUI];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    [self createUI];
}
- (void)createUI {
    self.view = [[[NSBundle mainBundle] loadNibNamed:@"PayView" owner:self options:nil] lastObject];
    self.view.frame = self.bounds;
    [self addSubview:self.view];
    
    // 设置默认选中支付方式
    [self.alipayButton setSelected:YES];
    
    // 半透明背景点击事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.backView addGestureRecognizer:tap];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [self setHidden:YES];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    // 确认按钮渐变色
    //[self.sureButton setGradientHorizontal:nil];
}
#pragma mark - IBAction
- (IBAction)cancelButtonAction:(id)sender {
    [self hiddenView];
}
- (IBAction)wechatButtonAction:(id)sender {
    [self.wechatButton setSelected:YES];
    [self.alipayButton setSelected:NO];
    [self.balanceButton setSelected:NO];
}
- (IBAction)alipayButtonAction:(id)sender {
    [self.wechatButton setSelected:NO];
    [self.alipayButton setSelected:YES];
    [self.balanceButton setSelected:NO];
}
- (IBAction)balanceButtonAction:(id)sender {
    [self.wechatButton setSelected:NO];
    [self.alipayButton setSelected:NO];
    [self.balanceButton setSelected:YES];
}
- (IBAction)sureButtonAction:(id)sender {
    [self hiddenView];
    NSString *payTypeStr = @"0";
    if (self.wechatButton.selected) {
        payTypeStr = @"0";
    }
    if (self.alipayButton.selected) {
        payTypeStr = @"1";
    }
    if (self.balanceButton.selected) {
        payTypeStr = @"2";
    }
    NSString *rechargeStr = @"0";
    if (!self.priceTF.hidden) {
        rechargeStr = [[self.priceTF.text componentsSeparatedByString:@"¥"] lastObject];
    } else {
        rechargeStr = [[self.priceLabel.text componentsSeparatedByString:@"¥"] lastObject];
    }
    
    if (self.block) {
        self.block(payTypeStr, rechargeStr);
    }
}
#pragma mark - action
- (void)tapAction:(UIGestureRecognizer *)sender {
    [self hiddenView];
}

- (void)showView {
    [self setHidden:NO];
    self.bottomViewBLayout.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self.backView setAlpha:1];
        [self.view layoutSubviews];
    } completion:^(BOOL finished) {
        
    }];
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:self.priceTF];
}
- (void)hiddenView {
    [self endEditing:YES];
    self.bottomViewBLayout.constant = -self.bottomView.mj_h;
    [UIView animateWithDuration:0.3 animations:^{
        [self.backView setAlpha:0];
        [self.view layoutSubviews];
    } completion:^(BOOL finished) {
        [self setHidden:YES];
    }];
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:self.priceTF];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if ([string isEqualToString:@"."] && [textField.text containsString:@"."]) {
        return NO;
    } else {
        return YES;
    }
    return YES;
}

- (void)textFiledEditChanged:(NSNotification *)obj {
    UITextField *textField = (UITextField *)obj.object;
    if (textField != self.priceTF) {
        return;
    }
    if ([textField.text hasPrefix:@"¥"]) {
        return;
    }
    textField.text = [NSString stringWithFormat:@"¥%@",textField.text];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    [self endEditing:YES];
}

#pragma mark -
- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:self.priceTF];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
